import { getPasswordCode, findPasswordCodeCheck, changePassword } from '@/api/user'
import Vue from '@/main'

const oldState = {
  // 手机号
  phone: '',
  // 手机号下方错误提示
  phoneErrorMessage: '',
  // 手机验证码
  code: '',
  // 错误提示信息
  errorMessage: '',
  // 设置密码所需的req_id
  req_id: '',
  // 新密码
  password: '',
  // 确认新密码
  confirm_password: '',
  // 新密码下方错误提示
  passwordErrorMessage: ''
}

const state = JSON.parse(JSON.stringify(oldState))

const mutations = {
  // 设置原始状态
  set_state (state) {
    for (const key in state) {
      state[key] = oldState[key]
    }
  },
  // 设置手机号
  set_phone (state, phone) {
    state.phone = phone
  },
  // 设置手机号下方错误提示
  set_phoneErrorMessage (state, msg) {
    state.phoneErrorMessage = msg
  },
  // 设置手机验证码
  set_code (state, code) {
    state.code = code
  },
  // 设置验证码下方错误提示
  set_errorMessage (state, msg) {
    state.errorMessage = msg
  },
  // 设置 设置密码所需的req_id
  set_req_id (state, id) {
    state.req_id = id
  },
  // 设置新密码
  set_password (state, password) {
    state.password = password
  },
  // 设置确认新密码
  set_confirm_password (state, confirmPassword) {
    state.confirm_password = confirmPassword
  },
  // 设置新密码下方错误提示
  set_passwordErrorMessage (state, msg) {
    state.passwordErrorMessage = msg
  }
}

const actions = {
  // 获取找回密码手机验证码
  getFindPasswordPhoneCode ({ state, commit, rootState }, params) {
    return new Promise((resolve, reject) => {
      let code
      if (params && params.captcha) {
        code = params.captcha
      }
      getPasswordCode(state.phone ? state.phone : rootState.user.userInfo.user_phone, code).then(res => {
        if (res.data.code === 200) {
          Vue.tools.countDown(60)
        } else if (res.data.code === 502) {
          if (!rootState.common.showImgDialog) {
            commit('common/set_showImgDialog', true, { root: true })
            commit('common/set_fnName', 'findPassword/getFindPasswordPhoneCode', { root: true })
            commit('common/set_fnParams', {}, { root: true })
            commit('common/set_imgCode', '', { root: true })
          } else {
          }
        } else {
          commit('set_phoneErrorMessage', res.data.message)
        }
        resolve(res)
      })
    })
  },
  // 检验手机验证码
  checkPhoneCode ({ state, commit, rootState }) {
    return new Promise((resolve, reject) => {
      findPasswordCodeCheck(state.phone ? state.phone : rootState.user.userInfo.user_phone, state.code).then(res => {
        switch (res.data.code) {
          case 200: {
            commit('set_req_id', res.data.data.req_id)
            break
          }
          case 500: {
            commit('set_phoneErrorMessage', res.data.message)
            break
          }
          case 501: {
            commit('set_errorMessage', res.data.message)
            break
          }
        }
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 设置新密码
  changePassword ({ state, commit, rootState }) {
    return new Promise((resolve, reject) => {
      if (state.password.trim() === '') {
        commit('set_passwordErrorMessage', '请输入新密码')
        return
      }
      if (state.confirm_password.trim() === '') {
        commit('set_errorMessage', '请再次输入新密码')
        return
      }
      changePassword(state.phone ? state.phone : rootState.user.userInfo.user_phone, state.password, state.confirm_password, state.req_id).then(res => {
        switch (res.data.code) {
          case 200: {
            break
          }
          case 412: {
            commit('set_passwordErrorMessage', res.data.message)
            break
          }
          case 500: {
            commit('set_errorMessage', res.data.message)
            break
          }
        }
        resolve(res)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
